package com.ygjy.managment.action;

import java.net.URLEncoder;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.sm.util.SmSessionUtil;
import com.util.FormatUtil;
import com.util.MD5;
import com.ygjy.pojo.SmUser;
import com.ygjy.service.SmUserService;

@Controller("loginAction")
@Scope("prototype")
public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = -1167662371883343271L;
	@Resource
	private SmUserService smUserService;
	
	private String rand;

	private String message;

	private String username;

	private String password;

	private String oldpassword;

	private String name;

	private String repassword;

	private String pkUser;

	private String ifupdateid;

	private String action;
	
	private MD5 md5 = new MD5();
	public String login() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String random = (String) session.getAttribute("random"); // 从session里获取验证码
		if (rand != null && !rand.toLowerCase().equals(random)) {
			message = URLEncoder.encode("验证码输入错误！", "UTF-8");
			return "noLogin";
		} else {
			String userName = "";
			if (username != null && !("").equals(username)) {
				userName = username.trim();
			}
			SmUser smuser = SmSessionUtil.getadminuser(request);
			if (smuser == null) {
				if (password != null && !("").equals(password)) {
					smuser = smUserService.findByUserAccount(userName, md5
							.calcMD5(password));
				}
				if (smuser == null) {
					message = URLEncoder.encode("用户名或密码错误！", "UTF-8");
					return "noLogin";
				} else {
					
					Date date = new Date();
					String sdate = FormatUtil.getFormatTime(
							FormatUtil.FORMAT_THREE, date);// 拿到系统当前时间
					smuser.setLastLoginTime(sdate);
					smUserService.update(smuser);
					SmSessionUtil.refurbishAdminUser(request, smuser);
					return "login";
				
				}
			} else {
				SmSessionUtil.invalidateAdmin(request, smuser);
				message = URLEncoder.encode("请在安全退出后再登陆其他账号！", "UTF-8");
				return "noLogin";
			}
		}
	}
   
	public String regist() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		SmUser loginuser = SmSessionUtil.getadminuser(request);
		if (loginuser != null) {
			if (username.trim() != null && !("").equals(username.trim())) {
				if (password.equals(repassword)) {
					SmUser suser = smUserService
							.findAdminUserIsExist(username.trim());
					if (suser == null) {
						suser = new SmUser();
						suser.setAccount(username.trim());
						suser.setPassword(md5.calcMD5(password));
						suser.setName(name);
						suser.setParentId(loginuser.getPkUser());
						Date date = new Date();
						String sdate = FormatUtil.getFormatTime(
								FormatUtil.FORMAT_THREE, date);
						suser.setAddTime(sdate);
						suser.setLastLoginTime(sdate);
						suser.setDef1(SmSessionUtil.getadminuser(request).getAccount());
						boolean flag = smUserService.save(suser,"pkUser");
						if (flag) {
							message = "添加用户成功！";
							return "list";
						}else {
							message="添加失败！";
		
						}
					}else {
						message="已经存在该用户！";
					
					}
				}else {
					message="两次设置的密码不同！";
			
				}
			}else{
				message="用户名不能为空！";
				
			}
			return "toAdd";
		}
		return "noLogin";
	}

	public String getAdminUser() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		if (pkUser != null && !"".equals(pkUser) ) {
			SmUser updateloginuser = smUserService.get(SmUser.class, pkUser);
			SmSessionUtil.refurbishAdminUser(request, updateloginuser);
		}
		return "noLogin";
	}

	

	public String logoutAdminUser() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		SmSessionUtil.removeAdminUser(request);
		return "noLogin";
	}
	
	

	public String updateAdminUser() throws Exception {
		message = "密码修改失败！";
		HttpServletRequest request = ServletActionContext.getRequest();
		SmUser loginuser = SmSessionUtil.getadminuser(request);
		if (loginuser != null) {
			if (!"".equals(oldpassword) && !"".equals(password)
					&& !"".equals(repassword)) {
				if (password.equals(repassword)) {
					SmUser smuser = smUserService.findByUserAccount(
							username, md5.calcMD5(oldpassword));
					if (smuser != null) {
						smuser.setPassword(md5.calcMD5(repassword));
						smuser.setName(name);
						boolean flag = smUserService.update(smuser);
						if (flag) {
							message = "密码修改成功！";
						}
						return "updateuser";
					}

				}
			} else {
				SmUser suser = smUserService.findAdminUserIsExist(username
						.trim());
				if (suser != null) {
					suser.setName(name);
					boolean flag = smUserService.update(suser);
					if (flag) {
						message = "修改成功！";
					}
				}
				return "updateuser";
			}

		}
		return "noLogin";
	}

	public String getRand() {
		return rand;
	}

	public void setRand(String rand) {
		this.rand = rand;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}

	public String getOldpassword() {
		return oldpassword;
	}

	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}

	public String getIfupdateid() {
		return ifupdateid;
	}

	public void setIfupdateid(String ifupdateid) {
		this.ifupdateid = ifupdateid;
	}
}
